package com.ruoyi.patrol.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.patrol.domain.PtCatalog;
import com.ruoyi.patrol.mapper.PtCatalogMapper;
import com.ruoyi.patrol.service.IPtCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyh
 */
@Service
public class PtCatalogServiceImpl extends ServiceImpl<PtCatalogMapper, PtCatalog> implements IPtCatalogService {
    @Autowired
    private PtCatalogMapper catalogMapper;

    @Override
    public List<PtCatalog> selectCatalogList() {
        List<PtCatalog> list = catalogMapper.selectCatalogList();
        return list;
    }

    @Override
    public int insertCatalog(PtCatalog catalog) {
        return catalogMapper.insertCatalog(catalog);
    }

    @Override
    public List<PtCatalog> buildCatalogTree(List<PtCatalog> list) {
        List<PtCatalog> treeList = new ArrayList<>();
        List<Long> catIds = list.stream().map(PtCatalog::getCatId).collect(Collectors.toList());

        for (PtCatalog catalog : list) {
            if(!catIds.contains(catalog.getParentCid())) {
                // 一级目录
                treeList.add(catalog);

                recursionFn(list, catalog );
            }
        }
        if(treeList.isEmpty()) {
            treeList = list;
        }
        return treeList;
    }

    @Override
    public int updateCatalog(PtCatalog catalog) {
        return catalogMapper.updateCatalog(catalog);
    }

    @Override
    public boolean hasChildByCatId(Long catId) {
        int row = catalogMapper.hasChildByCatId(catId);
        return row > 0;
    }

    @Override
    public int deleteByCatId(Long catId) {
        return catalogMapper.deleteCatalogById(catId);
    }

    @Override
    public PtCatalog getCatalogByCatId(Long catId) {
        PtCatalog catalog = catalogMapper.selectCatalogByCatId(catId);
        return catalog;
    }

    @Override
    public Long[] getCatalogPath(Long catId) {

        List<Long> paths = new ArrayList<>();

        List<Long> catalogPath = findParentPath(catId, paths);

        Collections.reverse(catalogPath);

        return catalogPath.toArray(new Long[catalogPath.size()]);

    }

    /**
     * 遍历
     * @param catId
     * @param paths
     * @return
     */
    private List<Long> findParentPath(Long catId, List<Long> paths) {
        paths.add(catId);
        PtCatalog catalog = catalogMapper.selectCatalogByCatId(catId);
        if(catalog.getParentCid() != 0) {
            findParentPath(catalog.getParentCid(), paths);
        }
        return paths;
    }

    private void recursionFn(List<PtCatalog> list, PtCatalog catalog) {
        // 获取当前节点的子节点
        List<PtCatalog> childrenList = getChildrenList(list, catalog);

        catalog.setChildren(childrenList);

        // 遍历子节点
        for (PtCatalog child : childrenList) {
            if(hasChild( list, child )) {
                recursionFn(list, child);
            }
        }


    }

    private boolean hasChild(List<PtCatalog> list, PtCatalog child) {
        return getChildrenList(list, child).size() > 0;
    }

    private List<PtCatalog> getChildrenList(List<PtCatalog> list, PtCatalog catalog) {
        List<PtCatalog> cList = new ArrayList<>();
        for (PtCatalog ptCatalog : list) {
            if(ptCatalog.getParentCid().longValue() == catalog.getCatId().longValue()) {
                cList.add(ptCatalog);
            }
        }
        return cList;
    }
}
